#include "scanmusic.h"


ScanMusic::ScanMusic(QObject *parent, \
                     const QString &path, const QString &format) :\
    QObject(parent)
{
    this->setFileFormat(format);
    thread = new QThread;
    connect(thread,&QThread::started,this,[this,path](){
        finMp3Files(path);
    });
    this->moveToThread(thread);
    thread->start();
}

void ScanMusic::finMp3Files(const QString &path)
{
    QDir dir(path);
    if (!dir.exists()) {
        qDebug() << "Directory does not exist:" << path;
        return;
    }
    QFileInfoList entries = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
    for (const QFileInfo &entry : entries) {
        if (entry.isFile()) {
            QString suffix = entry.suffix().toLower();
            if (suffix == this->getFileFormat()) {
                QStringList mp3InfoList;
                QString fileName = entry.fileName();
                scanLyric = new ScanLyric(this,fileName,entry.absoluteFilePath());
                mp3InfoList<<fileName<<entry.absoluteFilePath()\
                          <<scanLyric->getLrcName()<<scanLyric->getLrcAbsolutePath();
                emit mp3InfoReady(mp3InfoList);
            }
        } else if (entry.isDir()) {
            // 递归调用以搜索子目录
            finMp3Files(entry.absoluteFilePath());
        }
    }
    return ;
}

QString ScanMusic::getScanPath() const
{
    return scanPath;
}

void ScanMusic::setScanPath(const QString &value)
{
    scanPath = value;
}

QString ScanMusic::getFileFormat() const
{
    return fileFormat;
}

void ScanMusic::setFileFormat(const QString &value)
{
    fileFormat = value;
}
